<template>
  <div class="editAddress">
    <van-nav-bar title="新增地址" left-arrow @click-left="onClickLeft" />
    <van-address-edit
      :show-area="false"
      :show-delete="false"
      :tel-validator="validator"
      @save="onSave"
    />
  </div>
</template>
 <script>
export default {
  data(){
      return {
        name:sessionStorage.uName,
        id:sessionStorage.uId
      }
    },
  methods: {
    //返回上一页
    onClickLeft() {
      this.$router.push('/ipam');
    },
    //手机号码验证
    validator(event) {
      // console.log(event);
      let myreg = /^[1][3-9][0-9]{9}$/;
      if (!myreg.test(event)) {
        // console.log("手机号错误");
        return false;
      } else {
        // console.log('手机号正确')
        return true;
      }
    },
    //点击保存按钮时触发
    onSave(val) {
      console.log(val);
      console.log(this.name);
      console.log(this.id);
      let data = `aDsser=${val.name}&aPhone= ${val.tel}&aDs= ${val.addressDetail}&aName=${this.name}&aUid=${this.id}`;
      this.axios.post(`/kc/addto`, data).then((res) => {
        console.log(res);
        //获取url里查询字符串中的参数aDsser
        let adHerf = res.config.data;
        let adDate = adHerf.split('&');
        let aDsser = adDate[0].split('=')[1];
        // console.log(aDsser);
        //提示已成功添加
        this.$toast(`已成功添加${aDsser}`);
        //跳转到地址管理主页面
        this.$router.push('/Ipam');
      });
    },
  },
};
</script>
<style lang="scss">
.editAddress {
  text-align: left;
  height: 100vh;
  overflow: hidden;
  .van-icon {
    color: #000;
  }
  .van-nav-bar__text {
    color: #666;
  }
  .van-button--danger {
    color: #000;
    background-color: #ffcb00;
    border: 1px solid #ffcb00;
  }
}
</style>